package cn.rmc.org.controller;

import cn.rmc.basic.util.AjaxResult;
import cn.rmc.basic.util.Pagelist;
import cn.rmc.org.domain.Department;
import cn.rmc.org.domain.Employee;
import cn.rmc.org.query.EmployeeQuery;
import cn.rmc.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "/employee",description = "宠物之家组织结构模块部门管理的接口规范！")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/{id}")
    public Employee getById(@PathVariable("id")Long id){
        return employeeService.getById(id);
    }
    @GetMapping
    public List<Employee> getAll(){
        return employeeService.getAll();
    }
//    添加
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
//        两构造方法
        System.out.println(employee);
        try {
            if (employee.getId()==null){
                employeeService.add(employee);
            }else {
                employeeService.upadte(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
//    删除
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
//        使用静态方法
        try {
            employeeService.delete(id);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

//    高级查询
//    前台是通过Json对象传递进来的，而且是放到RequestBody
    @PostMapping
    public Pagelist<Employee> querys(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.query(employeeQuery);
    }

    @PatchMapping
    public AjaxResult delPatch(@RequestBody List<Long> ids){
        try {
            employeeService.delpatch(ids);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
    @GetMapping("/tree")
    public List<Employee> findDepyTree(){
        return employeeService.findEmpTree();
    }
}
